# -*- coding:utf-8 -*-
# @Time  : 2019/11/29 19:29
# @Author: wangxb
'''
根据指定本地apk名称，抓取微信ID
'''
import os
import time

from classes.jarOperation import generateJarFile, examZipFile, getAllClassesFileInAPK, changeFileSuffix, copyAPKToZip, \
    killJdgui
from classes.searchContent import examineDirectory, read_zipFile, FindConetent
from classes.threadOperation import JdguiThread, KeyboardThread

# apk包名
apk_name = 'com.youku.phone_9.3.0_241.apk'
# 项目的根路径
root_path = os.path.dirname(os.getcwd())
# apk文件路径
apk_file_path = root_path + os.sep + 'APKSources' + os.sep + apk_name
# apk的zip文件路径
apk_zip_file_path = changeFileSuffix(apk_file_path)
# 反编译后的zip文件路径
jar_zip_file_path = root_path + os.sep + 'zipSources'
# lanchon_tools工具所在路径
lanchon_tools_path = root_path + os.sep + 'dex-tools-lanchon'
# 搜索文件的根路径
searchRootPath = root_path + os.sep + 'zipSources' + os.sep + 'classes-dex2jar.src'
# 搜索结果输出路径
outputPath = root_path + os.sep + 'output_files.txt'


def totalProcess():
    f = open(outputPath, "a", encoding='latin-1')
    try:
        # 获取对应apk的zip包
        copyAPKToZip(apk_file_path, apk_zip_file_path)
        # 获取APK对应zip包中的classes.dex文件
        # classes_list = []
        classes_list = getAllClassesFileInAPK(apk_zip_file_path)

        # 在输出文件中区分每个app的微信ID
        # f = open(outputPath, "a", encoding='latin-1')
        f.write('#############################Start##################################')
        # f.close()

        for classes_file in classes_list:
            try:
                print(
                    '\n#############################Start decompile ' + classes_file + '#############################')
                # 生成apk对应的jar包文件
                generateJarFile(apk_zip_file_path, lanchon_tools_path, classes_file)

                # 调用jd-gui工具
                t = JdguiThread("jdguiThread", lanchon_tools_path)
                t.start()

                # 判断dex2jar的zip文件是否存在，存在则删除
                examZipFile(jar_zip_file_path)
                time.sleep(2)

                # 调用快捷键，另存为classes-dex2jar.src.zip
                t2 = KeyboardThread("keyboardThread", jar_zip_file_path)
                t2.start()

                # 主线程等25s，保存大的zip文件
                time.sleep(30)

                # 检测jd-gui进程是否还存在。存在返回0，则超时结束进程
                killJdgui()

                # 避免[WinError 32]提示另一个程序正在使用此文件，进程无法访问异常
                time.sleep(2)

                # 删除之前搜索目录中遗留的文件
                examineDirectory(searchRootPath)

                # 解压classes-dex2jar.src.zip文件
                read_zipFile(jar_zip_file_path, searchRootPath)

                # 根据关键字搜索指定路径下所有文件的内容
                findObj = FindConetent(searchRootPath)
                findObj.find_file(findObj)
                findObj.walk(findObj, outputPath, apk_file_path)
            except BaseException as e:
                print(e)
            finally:
                # 删除对应apk的zip包
                killJdgui()

        # f = open(outputPath, "a", encoding='latin-1')
        f.write('#############################End##################################\n')
    except BaseException as error:
        print(error)
    finally:
        # 删除对应apk的zip包
        os.remove(apk_zip_file_path)
        f.close()


def search():
    try:
        # 在输出文件中区分每个app的微信ID
        f = open(outputPath, "a", encoding='latin-1')
        f.write('#############################Start##################################')
        f.close()
        searchRootPath = 'C:\\Users\\99683\\Downloads\\jadx-1.1.0\\bin\\testJar\\sources'
        # 根据关键字搜索指定路径下所有文件的内容
        findObj = FindConetent(searchRootPath)
        findObj.find_file(findObj)
        findObj.walk(findObj, outputPath, apk_file_path)
        # 在输出文件中区分每个app的微信ID
        f = open(outputPath, "a", encoding='latin-1')
        f.write('#############################Start##################################')
    except BaseException as error:
        print(error)
    # finally:
    #     f.close()


if __name__ == '__main__':
    # 走全部流程，包含apk=>zip=>dex2jar=>jd-gui=>search
    # totalProcess()
    # 只对zipSources/classes-dex2jar.src目录进行搜索
    search()
